package com.mdinic.blogovi.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Configurable
@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findSharesByBlogPost", "findSharesByPerson" })
public class Share implements Serializable {

    private static final long serialVersionUID = 1L;

    @NotNull
    @ManyToOne
    private BlogPost blogPost;

    @NotNull
    @ManyToOne
    private Person person;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    @PersistenceContext
    transient EntityManager entityManager;

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return this.version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public static final EntityManager entityManager() {
        EntityManager em = new Share().entityManager;
        if (em == null)
            throw new IllegalStateException(
                    "Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countFollowShares() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Share o", Long.class).getSingleResult();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Share attached = Share.findShare(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public com.mdinic.blogovi.model.Share merge() {
        if (this.entityManager == null)
            this.entityManager = entityManager();
        Share merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public BlogPost getBlogPost() {
        return this.blogPost;
    }

    public void setBlogPost(BlogPost blogPost) {
        this.blogPost = blogPost;
    }

    public Person getPerson() {
        return this.person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public static long countShares() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Share o", Long.class).getSingleResult();
    }

    public static List<Share> findAllShares() {
        return entityManager().createQuery("SELECT o FROM Share o", Share.class).getResultList();
    }

    public static Share findShare(Long id) {
        if (id == null)
            return null;
        return entityManager().find(Share.class, id);
    }

    public static List<Share> findShareEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Share o", Share.class).setFirstResult(firstResult)
                .setMaxResults(maxResults).getResultList();
    }

    public static TypedQuery<Share> findSharesByBlogPost(BlogPost blogPost) {
        if (blogPost == null)
            throw new IllegalArgumentException("The blogPost argument is required");
        EntityManager em = Share.entityManager();
        TypedQuery<Share> q = em.createQuery("SELECT o FROM Share AS o WHERE o.blogPost = :blogPost", Share.class);
        q.setParameter("blogPost", blogPost);
        return q;
    }

    public static TypedQuery<Share> findSharesByPerson(Person person) {
        if (person == null)
            throw new IllegalArgumentException("The person argument is required");
        EntityManager em = Share.entityManager();
        TypedQuery<Share> q = em.createQuery("SELECT o FROM Share AS o WHERE o.person = :person", Share.class);
        q.setParameter("person", person);
        return q;
    }

    public static List<Share> findSharesByPostAndPerson(BlogPost blogPost, Person person) {
        if (person == null)
            throw new IllegalArgumentException("The person argument is required");
        if (blogPost == null)
            throw new IllegalArgumentException("The blogPost argument is required");
        EntityManager em = Share.entityManager();
        TypedQuery<Share> q = em.createQuery(
                "SELECT o FROM Share AS o WHERE o.person = :person AND o.blogPost = :blogPost", Share.class);
        q.setParameter("person", person);
        q.setParameter("blogPost", blogPost);
        return q.getResultList();
    }
}
